<?php

class ReportController extends Controller
{
    public function actionIndex()
    {
		die('work');
        $this->render('index');
    }

    public function actionTax()
    {
        $this->render('tax');
    }

    public function actionList($custno, $jobsalesperson=null,$year=null,$page = 0, $sort = NULL, $status = Null, $startdate = Null, $end = Null)
    {

        if ($page > 0) $page--;
        $order = CJSON::decode($sort);
        $criteria = new CDbCriteria();
        if ($custno != 'All') {
            $criteria->condition = 'custno=:custno';
            $criteria->params = array(
                'custno' => $custno);
        }
        if($jobsalesperson !=null){
            $criteria->compare('jobsalesperson', $jobsalesperson);
        }
        if ($status != 'All') $criteria->addColumnCondition(array('status' => intval($status)));
        if($year!=null){
            $criteria->addColumnCondition(array('year(addeddate)'=>$year));
        }

        if (!empty($startdate) && !empty($end)) {
            $fstartdate = explode('/', $startdate);
            $startdate = $fstartdate['2'] . '-' . $fstartdate['0'] . '-' . $fstartdate['1'];
            $fend = explode('/', $end);
            $end = $fend['2'] . '-' . $fend['0'] . '-' . $fend['1'];
            $criteria->addBetweenCondition('addeddate', $startdate, $end);

        }else if($year!=null){
            $criteria->addColumnCondition(array(
                'year(addeddate)'=>$year
            ));
        }
        $criteria->order = isset($order) ? $order[0]['property'] . ' ' . $order[0]['direction'] : NULL;
        $data = new CActiveDataProvider('Jobs', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20,
                'currentPage' => $page
            )
        ));

        if ($data != NULL && count($data->getData()) > 0) {
            $rs = array();
            $rs['totalCount'] = $data->getTotalItemCount();
            foreach ($data->getData() as $i => $item) {

                $rs['data'][$i] = $item->getAttributes();
                $rs['data'][$i]['locname'] = $item->location->locname;
                $rs['data'][$i]['billingstatustext'] = JobsBillingStatus::getText($item->billingstatus);
                $rs['data'][$i]['jobstatustext'] = JobsStatus::getText($item->jobstatus);

            }
            echo json_encode($rs);
        }
        Yii::app()->end();
    }

     public function actionContractReport($custno){
        $data       = array();
        $globalContacts = Locationcontacts::model()->findAll("locno='00000' and custno='$custno'");
        foreach ($globalContacts as $contact)
        {
            $data[$contact->email] = 'G: '.$contact->contact . ' - ' . $contact->email . ' - ' . $contact->jobtitle;
        }
        $presidentContract = Employees::model()->findByPk('1109');
        if ($presidentContract != null)
        {
            $data[$presidentContract->workemail] = 'Greg Cahoe' . ' - ' . $presidentContract->workemail;
        }

        if (empty($data))
            $data = array('N/A', 'N/A');
        outputJsByList($data, 'contacts');
       $this->render('contract',array('custno'=>$custno));
    }
}
